package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.AbstractListModel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import officeserver.log_error.UserException;
import officeserver.report.Appointment;
import officeserver.report.Treatment;
import officeserver.users.AccessLevel;
import officeserver.users.PersonalInfo;

import GUI.calendar.CalendarPanel;
import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.PTextField;
import GUI.helpers.ProjectConstants;
import GUI.helpers.TextFieldType;
import GUI.main.MainClient;
import GUI.main.renderers.TreatmentListRenderer;
/**
 * 
 * @author So-mi
 *
 */
public class ReadOnlyTreatmentDialog extends JDialog implements ActionListener, MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel titlePanel;    
    private JTabbedPane tabPane;
    
    private JPanel labWorkPanel;
    private JPanel perscriptionPanel;
	
    
    private JPanel labDataPanel;
    
    private JPanel patientPanel;
    private JLabel patientLabel;
    private PTextField patientField;
    
    private JPanel doctorPanel;
    private JLabel doctorLabel;
    private PTextField doctorField;
    
    private JPanel nursePanel;
    private JLabel nurseLabel;
    private PTextField nurseField;
    
    
//    private Appointment appointment;
    
    private JPanel buttonPanel;
    private PButton closeButton;
    
    private static final int WIDTH = 400;    
    private static final int HEIGHT = 500;
	
    /**
     * Creates a general format of the ReadOnlyTreatmentDialog
     * It's a BorderLayout with North (title), Center (main data), South (close button)
     */
    public ReadOnlyTreatmentDialog(){
 //   this.appointment = appointment;
	setLayout(new BorderLayout());
	setSize(new Dimension(WIDTH,HEIGHT));
	add(getTitlePanel(),BorderLayout.NORTH);
	add(getTabbedPanel(),BorderLayout.CENTER);
	add(getButtonPanel(), BorderLayout.SOUTH);
	}
    
    /**
     * Creates the bar at the top with the name of what the panel is
     * 
     * @return Text that says "Treatments" at the top
     */
    private JPanel getTitlePanel(){
    if(titlePanel == null){
        titlePanel = new JPanel(){
    	private static final long serialVersionUID = 1L;

    	public void paintComponent(Graphics g) {
    	    super.paintComponent(g);
    	    g.setColor(ProjectConstants.SUBHEADER_COLOR);
    	    g.fillRect(0, 0, getWidth(), getHeight());
    	    g.setColor(Color.white);
    	    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
    	    g.drawString("treatments: ", 10, getHeight() - 8);
    	}
    	};
    	titlePanel.setPreferredSize(new Dimension(WIDTH,
    			ProjectConstants.HEADER_HEIGHT));
    }
    return titlePanel;    	
    }
    
    /**
     * Creates tabbedPane for Treatment and LabWork
     * @return tabs for Treatment and Labwork
     */
    private JTabbedPane getTabbedPanel(){
   	if(tabPane == null){
   	    tabPane = new JTabbedPane();
   	    tabPane.addTab("lab work", getLabWorkPanel());
   	    tabPane.addTab("perscription", getPerscriptionPanel());
   	}
   	return tabPane;
    }    	
    
    /**
     * Creates a LabWorkPanel that contains information regarding Labwork
     * @return data about Labworks
     */
    private JPanel getLabWorkPanel(){
    if(labWorkPanel == null){
        labWorkPanel = new JPanel();
        labWorkPanel.setLayout(new BoxLayout(labWorkPanel,
    		    BoxLayout.PAGE_AXIS));
	    labWorkPanel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
	    labWorkPanel.add(getLabWorkDataPanel());
    }
    return labWorkPanel;
    }
    
    /**
     * 
     * @return
     */
    private JPanel getLabWorkDataPanel(){
    if (labDataPanel == null) {
   	    labDataPanel = new JPanel();
   	    labDataPanel.setLayout(new BoxLayout(labDataPanel,
   		    BoxLayout.PAGE_AXIS));
   	    
   	    labDataPanel.add(getPatientPanel());
   	    labDataPanel.add(getDoctorPanel());
   	    labDataPanel.add(getNursePanel());
    }
    return labDataPanel;
    }
   
    /**
     * 
     * @return
     */
    private JPanel getPatientPanel() {
    if (patientPanel == null) {
        patientPanel = new JPanel();
        patientPanel.setBorder(new EmptyBorder(10, 10, 0, 10));
        patientPanel.setLayout(new BorderLayout());
        patientPanel.add(getPatientLabel(), BorderLayout.WEST);
        patientPanel.add(getPatientField(), BorderLayout.CENTER);
        patientPanel.addMouseListener(this);
    }
    return patientPanel;
    }
    
    /**
     * 
     * @return
     */
    private JLabel getPatientLabel() {
   	if (patientLabel == null) {
   	    patientLabel = new JLabel(ProjectConstants.CELLPAD + "patient:");
   	    patientLabel.setPreferredSize(new Dimension(
   		    ProjectConstants.LABEL_WIDTH_A, 25));
   	    patientLabel.addMouseListener(this);
   	}
   	return patientLabel;
    }
    
    /**
     * 
     * @return
     */
    private PTextField getPatientField() {
    if (patientField == null) {
        /*try {
    	MainClient.getConnection().userSearch(
    		appointment.getPatientID(), AccessLevel.PATIENT);
        } catch (UserException e) {
    	e.printStackTrace();
        }
        PersonalInfo pInfo = null;
        try {
    	pInfo = MainClient.getConnection().getSearchPersonalInfo();
        } catch (UserException e) {
    	e.printStackTrace();
        }
        patientField = new PTextField(pInfo.getFirstName() + " "
    	    + pInfo.getLastName(), TextFieldType.LONG);
        patientField.addMouseListener(this);*/
    	patientField = new PTextField("Patient name", TextFieldType.LONG);
    	patientField.setEditable(false);
    }
    return patientField;
    }  
    
    /**
     */
    private JPanel getDoctorPanel() {
    if (doctorPanel == null) {
        doctorPanel = new JPanel();
        doctorPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        doctorPanel.setLayout(new BorderLayout());
        doctorPanel.add(getDoctorLabel(), BorderLayout.WEST);
        doctorPanel.add(getDoctorField(), BorderLayout.CENTER);
        doctorPanel.addMouseListener(this);
    }
    return doctorPanel;
    }

    /**
    * @return
    */
    private JLabel getDoctorLabel() {
    if (doctorLabel == null) {
        doctorLabel = new JLabel(ProjectConstants.CELLPAD + "doctor:");
        doctorLabel.setPreferredSize(new Dimension(
    	    ProjectConstants.LABEL_WIDTH_A, 25));
        doctorLabel.addMouseListener(this);
    }
    return doctorLabel;
    }

    /**
    * @return
    */
    private PTextField getDoctorField() {
    if (doctorField == null) {
/*      try {
    	MainClient.getConnection().userSearch(
    		appointment.getDoctorID(), AccessLevel.DOCTOR);
        } catch (UserException e) {
    	e.printStackTrace();
        }
        PersonalInfo pInfo = null;
        try {
    	pInfo = MainClient.getConnection().getSearchPersonalInfo();
        } catch (UserException e) {
    	e.printStackTrace();
        }
        doctorField = new PTextField(pInfo.getFirstName() + " "
    	    + pInfo.getLastName(), TextFieldType.LONG);
        doctorField.addMouseListener(this);**/
    	doctorField = new PTextField("Doctor's name", TextFieldType.LONG);
    	doctorField.setEditable(false);
    }
    return doctorField;
    }
    
    /**
     * 
     * @return
     */
    private JPanel getNursePanel() {
    if (nursePanel == null) {
        nursePanel = new JPanel();
        nursePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        nursePanel.setLayout(new BorderLayout());
        nursePanel.add(getNurseLabel(), BorderLayout.WEST);
        nursePanel.add(getNurseField(), BorderLayout.CENTER);
        nursePanel.addMouseListener(this);
    }
    return nursePanel;
    }
    
    /**
     * 
     * @return
     */
    private JLabel getNurseLabel() {
    if (nurseLabel == null) {
    	nurseLabel = new JLabel(ProjectConstants.CELLPAD + "nurse:");
        nurseLabel.setPreferredSize(new Dimension(
            ProjectConstants.LABEL_WIDTH_A, 25));
        nurseLabel.addMouseListener(this);
    }
    return nurseLabel;
    }
 
    /**
     * 
     * @return
     */
    private PTextField getNurseField(){   
    if (nurseField == null) {
    	nurseField = new PTextField("Nurse's Name", TextFieldType.LONG);
    	nurseField.setEditable(false);
    }
    return nurseField;
    }
    
    
    
    
    /**
     * Creates a PrescriptionPanel that contains information regarding prescriptions
     * @return data about prescriptions
     */
    private JPanel getPerscriptionPanel(){
    if(perscriptionPanel == null){
        perscriptionPanel = new JPanel();
        perscriptionPanel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
    }
    return perscriptionPanel;
    }
    
    /**
     * creates the "close" button at the bottom.
     * @return
     */
    private JPanel getButtonPanel(){
    if(buttonPanel == null){
        buttonPanel = new JPanel();
        
        buttonPanel.setBorder(new EmptyBorder(10,10,10,10));
    	    
        buttonPanel.add(getCloseButton());
    }
    return buttonPanel;
    }
       
    private PButton getCloseButton(){
    if(closeButton == null){
        closeButton = new PButton("Close",ButtonType.MEDIUM);
        closeButton.addActionListener(this);
    }
    return closeButton;
    }
    
       
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
